import os
import sys
import threading

base_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
file_path = os.path.join(base_dir, 'configs/business_config.txt')

class BusinessConfig:
    _instance = None
    _lock = threading.Lock()  # 创建线程锁

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(BusinessConfig, cls).__new__(cls)
            # 检查文件是否存在
            if not os.path.exists(file_path):
                print('配置文件name_config.txt未找到。')
                raise FileNotFoundError(f"配置文件 {file_path} 未找到。")
            data = cls._instance.read_data()
            cls._instance.data = data
        return cls._instance

    def read_data(self):
        data = {}
        with open(file_path, 'r', encoding='utf-8') as file:
            for line in file:
                line = line.strip()
                if line:  # 忽略空行
                    # data_list.append(line)
                    key, value = line.split("=")  # 分割键和值
                    data[key.strip()] = eval(value.strip())  # 使用 eval 将字符串转换为实际数据类型
        return data
